<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
//课程表控制器
//LSvKing @2009.10.19
class Syllabus extends Controller {

    function Syllabus() {
        parent::Controller();
        $this->load->library('table');
        $this->load->model('syllabus/syllabus_model');
        $this->load->model('dx_auth/roles','roles');
    }

    function index() {
    //代码
        $this->searchsyll();
    }
    //查看课表
    function searchsyll() {
        $data['search']=true;
        $data['classid']=$this->syllabus_model->getClass()->result();
        $data['year']=$this->syllabus_model->getyear();
        $data['info']='等待查询..';
        //判断是否提交
        if($this->input->post('year')&&$this->input->post('classid')) {
            $data['search']=false;
            $year=$this->input->post('year');
            $classid=$this->input->post('classid');
            $data['current_class']=$classid;
            $data['current_year']=$year;
            $getCourse=array(
                'year'=>$year,
                'classid'=>$classid
            );
            $data['course']=array();
            //获取课表字串
            $query=$this->syllabus_model->getsyllabus_by_class($getCourse);
            //倘若没有课表
            if(!$query->result()) {
                $data['search']=true;
                $data['info']='本学期还没有本班的课表，请在“添加课程”中添加！';
                $this->load->view('admin/syllabus',$data);
                return ;
            }else {
                $result=array();
                foreach($query->result() as $row) {
                    $result[]=$row->content;
                    $data['syllabus_id']=$row->id;
                }
            }
            //将课表字串转为数组
            $syllabus_list=explode(',',$result[0]);
            $list=array();
            foreach($syllabus_list as $i) {
                $list[]=$this->syllabus_model->getcourse_name_by_id($i);
            }
            $v=1; //周期计数器
            $syllabus[$v]=array();
            //获取学期下的所有课程
            $dropdown[$v]=array();
            $select=array();
            //建立select选框
            $result=$this->syllabus_model->getcourse_by_year($year);
            foreach($result->result() as $row) {
                $select[$row->id]=$row->coursename;
            }
            //设置属性隐藏
            $property='class="syllabus_input"';
            $select['0']='没有课';
            //每5天创建一个数组
            for($i=0;$i<count($list);$i++) {
                $syllabus[$v][]=$list[$i];
                $dropdown[$v][]=form_dropdown('course[]',$select,$syllabus_list[$i],$property);
                if(($i+1)%5==0) {
                    $v++;
                }
            }
            $data['course']=array();
            //对应输出
            for($i=1;$i<=8;$i++) {
                $data['course'][]=array(
                    //硬读取
                    '<span class="syllabus_text">'.$syllabus[$i][0].'</span>'.$dropdown[$i][0],
                    '<span class="syllabus_text">'.$syllabus[$i][1].'</span>'.$dropdown[$i][1],
                    '<span class="syllabus_text">'.$syllabus[$i][2].'</span>'.$dropdown[$i][2],
                    '<span class="syllabus_text">'.$syllabus[$i][3].'</span>'.$dropdown[$i][3],
                    '<span class="syllabus_text">'.$syllabus[$i][4].'</span>'.$dropdown[$i][4]
                );
            }
        }
        $this->load->view('admin/syllabus',$data);
    }
    //获取课表课程
    function getsyllabus($year=false,$class=false) {
        $this->load->model('dx_auth/users', 'users');
        //获取班级与学期
        $data['year']=$this->syllabus_model->getyear();
        $data['classid']=$this->syllabus_model->getClass()->result();
        $data['info']='请选择..';
        $data['search']=false;
        if(!$year) {
            if($this->input->post('year')&&$this->input->post('classid')) {
                $year=$this->input->post('year');
                $class=$this->input->post('classid');
                //获取某学期的所有课程，构建下拉菜单

                $add_options=array(
                    'class'=>$class,
                    'year'=>$year
                );
                $result=$this->syllabus_model->getcourse_by_year($year);
                if($this->syllabus_model->syllabus_is_exist($add_options)) {
                    $data['info']='课表已存在,请到“查询课表”查看';
                    $this->load->view('admin/getsyllabus',$data);
                    return ;
                }
                $data['course']=array();
                $data['currentyear']=$year;
                $data['currentclass']=$class;
                $dropdown=array();
                foreach($result->result() as $row) {
                    $dropdown[$row->id]=$row->coursename;
                }
                $dropdown['0']='没有课';
                $dropdown=form_dropdown('course[]',$dropdown,'0');
                //创建一天8节课的选项表
                for($i=1;$i<=8;$i++) {
                    $data['course'][]=array(
                        $i,$dropdown,$dropdown,$dropdown,$dropdown,$dropdown
                    );
                }
            }else {
                $result=$this->load->view('admin/getsyllabus',$data);
                return;
            }
        }else {
            $year=$year;
        }
        $data['search']=true;
        $this->load->view('admin/getsyllabus',$data);
    }
    ////添加课表////
    function addsyllabus() {
        if($this->input->post('course')&&$this->input->post('classid')) {
            $syllabus=array();
            $syllabusList=$this->input->post('course');
            $classid=$this->input->post('classid');
            //讲课表数组转换为字符串储存
            $syllabus=implode(',',$syllabusList);
            $currentyear=$this->input->post('currentyear');
            $add_options=array(
                'class'=>$classid,
                'year'=>$currentyear,
                'content'=>$syllabus
            );
            if($this->syllabus_model->addsyllabus($add_options)) {
            //创建课程目录
                $this->create_course_dir($currentyear,$classid,$syllabusList);
                $data['info']='添加成功,正在返回..';
                $data['url']=site_url('admin/syllabus/getsyllabus');
                $this->load->view('admin/info',$data);
                return ;
            }
        }else {
            $data['info']='提交失败,正在返回..';
            $this->load->view('admin/info',$data);
            return ;
        }
    }

    ///编辑课表////
    function editsyllabus() {
    //获取学期
        $currentyear=$this->input->post('year');
        //获取班级
        $classid=$this->input->post('class');
        //获取课程表
        $syllabusList=$this->input->post('course');
        $syllabus=implode(',',$syllabusList);
        $id=$this->input->post('syllabus');
        $options=array(
            'content'=>$syllabus,
            'id'=>$id,
            'classid'=>$classid,
            'year'=>$currentyear
        );
        if($this->syllabus_model->update_syllabus($options)) {
            $this->create_course_dir($currentyear,$classid,$syllabusList);
            $data['info']='修改成功，正在返回...';
        }else {
            $data['info']='修改失败，正在返回...';
        }
        $data['url']=site_url('admin/syllabus/syllabus');
        $this->load->view('admin/info',$data);
    }

    ///删除课表///
    function delete_syllabus($id) {
        $result=$this->syllabus_model->delete_syllabus($id);
        if($result) {
            $data['info']='删除成功,正在返回..';
        }else {
            $data['info']='删除失败,正在返回..';
        }
        $data['url']=site_url('admin/syllabus/syllabus');
        $this->load->view('admin/info',$data);
    }

    /////添加课程///////
    function addcourse() {
        $this->load->model('dx_auth/users', 'users');
        //如果提交添加，进行处理
        if($this->input->post('coursename')&&$this->input->post('teacherid')) {
        //post获取
            $year=$this->input->post('year');
            $courseList=$this->input->post('coursename');
            $teacherList=$this->input->post('teacherid');
            $coursename=array();
            $teacherid=array();
            //筛选input
            for($i=0;$i<count($courseList);$i++) {
                $array=array(
                    'course'=>$courseList[$i],
                    'teacher'=>$teacherList[$i],
                    'year'=>$year,
                );
                if(!$courseList[$i]||!$teacherList[$i]) {
                    continue;
                }elseif($this->syllabus_model->course_is_exist($array)->result()) {
                    continue;
                }else {
                //重组数组
                    $teacherid[]=$teacherList[$i];
                    $coursename[]=$courseList[$i];
                    //获取id用户的整条数据
                    $k=$this->users->get_user_by_id($teacherList[$i])->result();
                    //将搜索后数据的用户名写入数组
                    $showdata['teachername'][]=$k[0]->username;
                }
            }
            $addCourse_options=array(
                'teacherid'=>$teacherid,
                'coursename'=>$coursename,
                'year'=>$year
            );
            //插入课程
            $this->syllabus_model->addCourse($addCourse_options);
            $showdata['coursename']=$coursename;
            $showdata['result'][]=true;
            $showdata['result'][]='addCourse';
            $query=$this->syllabus_model->getyear_by_id($year);
            foreach($query->result() as $row) {
                $showdata['year']=$this->syllabus_model->replace_year($row->year);
                $year=$row->year;
            }
            //插入数据
            $this->load->view('admin/su',$showdata);
        //未提交载入基本页面
        }else {
        //获取权限为教师的所有用户
            $data['teacherid']=$this->users->get_user_by_roles(3)->result();
            //获取现有的学年
            $data['year']=$this->syllabus_model->getyear();
            //载入视图
            $this->load->view('admin/addcourse',$data);
        }
    }
    ////获取课程///////////////
    function getcourse($year=false) {
    //判断是否提交
        $data['year']=$this->syllabus_model->getyear();
        //如果判断
        $data['search']=false;
        if(!$year) {
            if($this->input->post('year')) {
                $year=$this->input->post('year');
            }else {
                $this->load->view('admin/getcourse',$data);
                return;
            }
        }else {
            $year=$year;
        }
        $this->load->model('dx_auth/users', 'users');
        //获取课程列表
        $result=$this->syllabus_model->getcourse_by_year($year);
        $data['course']=array();
        $data['currentyear']=$year;
        foreach($result->result() as $row) {
        //获取老师姓名
            $teacherid=$row->teacherid;
            //错误处理，当老师被删除时
            $teacher=$this->users->get_user_by_id($teacherid)->result();
            if(!$teacher) {
                $teacher='<span style="color:red">教师已被删除</span>';
            }else {
                $teacher=$teacher[0]->username;
            }

            //拼接数组
            $checkbox = array(
                'class'  => 'checkbox',
                'value'  => $row->id,
                'name'  => 'courseid[]'
            );
            //添加批量删除复选框
            $data['course'][]=array(
                form_checkbox($checkbox),
                $row->id,
                $row->coursename,
                $teacher,
                //根据课程参数修改
                "<a href='editcourse/$row->id/$teacherid/$year'/>编辑</a>"
            );
        }
        $data['search']=true;
        $this->load->view('admin/getcourse',$data);
    }
    ///////删除课程////////////
    function deletecourse() {
        if($this->input->post('courseid')) {
            $courseid=$this->input->post('courseid');
            $this->syllabus_model->deletecourse($courseid);
            $this->getcourse($this->input->post('currentyear'));
        }else {
            $this->getcourse();
        }
    }
    //////编辑课程/////////
    function editcourse($courseid,$teacherid=false,$year) {
        if($this->input->post('coursename')) {
            $teacherid=$this->input->post('teacherid');
            $coursename=$this->input->post('coursename');
            $this->syllabus_model->editcourse($courseid,$coursename,$teacherid);
            //返回搜索页面
            $this->getcourse();
        }else {
            $this->load->model('dx_auth/users', 'users');
            $data['teacherid']=$this->users->get_user_by_roles(3)->result();
            //获取课程信息
            $result=$this->syllabus_model->getcourse_by_id($courseid);
            $data['course']=$result;
            $data['year']=$year;
            $data['teacher']=$teacherid;
            $this->load->view('admin/editcourse',$data);
        }
    }
    ///工具函数//
    //去除数组中所有相同项//
    function find_same($array) {
        $a=$array;
        $k=array();
        $c=array();
        for($i=0;$i<count($a);$i++) {
            for($j=0;$j<count($a);$j++) {
                if($a[$i]==$a[$j]) {
                    $k[$a[$i]]=$a[$i];
                }
            }
        }
        foreach($k as $row) {
            $c[]=$row;
        }
        return $c;
    }
    //创建课程目录
    function create_course_dir($currentyear,$classid,$syllabusList) {
    //创建课程目录
        $classid=$this->syllabus_model->getClass_by_id($classid)->result();
        foreach($classid as $row){
            $classid=$row->name;
        }
        $path='./uploads/assign/';
        $query=$this->syllabus_model->getyear_by_id($currentyear);
        foreach($query->result() as $row) {
            $dir_name=$row->year;
        }
        $coursename=$this->find_same($syllabusList);
        foreach($coursename as $row) {
        //不存在的话创建班级目录
            $course_name=$this->syllabus_model->getcourse_name_by_id($row);
            if($course_name=='没有课') {
                continue;
            }
            $course_dir=iconv("UTF-8", "GB2312",$course_name);
            if(!file_exists("$path/$dir_name/$classid")) {
                mkdir("$path/$dir_name/$classid");
            }
            //创建课程目录
            if(!file_exists("$path/$dir_name/$classid/$course_dir")) {
                mkdir("$path/$dir_name/$classid/$course_dir");
            }
        }
    }
}
?>